﻿/*!
 * ScanSnap JavaScript Library
 * Product: PFU LIMITED
 *
 * Copyright PFU LIMITED 2018-2019
 * Version: 1.0.1
 */
(function() {

    window['fujitsu'] = {
        scansnap: {}
    };
    Object.defineProperty(fujitsu.scansnap, "bwBrightness", {
		get: function() {
		    return ssHub.state.bwBrightness;
		},
		set: function(v) {
		    ssHub.state.bwBrightness = v;
		}
	});
    Object.defineProperty(fujitsu.scansnap, "contrast", {
		get: function() {
		    return ssHub.state.contrast;
		},
		set: function(v) {
		    ssHub.state.contrast = v;
		}
	});
    Object.defineProperty(fujitsu.scansnap, "compressionType", {
		get: function() {
		    return ssHub.state.compressionType;
		},
		set: function(v) {
		    ssHub.state.compressionType = v;
		}
	});
    Object.defineProperty(fujitsu.scansnap, "fileName", {
		get: function() {
		    return ssHub.state.fileName;
		},
		set: function(v) {
		    ssHub.state.fileName = v;
		}
	});
    Object.defineProperty(fujitsu.scansnap, "saveFolder", {
		get: function() {
		    return ssHub.state.saveFolder;
		},
		set: function(v) {
		    ssHub.state.saveFolder = v;
		}
	});
    Object.defineProperty(fujitsu.scansnap, "format", {
		get: function() {
		    return ssHub.state.format;
		},
		set: function(v) {
		    ssHub.state.format = v;
		}
	});
    Object.defineProperty(fujitsu.scansnap, "jpegQuality", {
		get: function() {
		    return ssHub.state.jpegQuality;
		},
		set: function(v) {
		    ssHub.state.jpegQuality = v;
		}
	});
    Object.defineProperty(fujitsu.scansnap, "pageCount", {
		get: function() {
		    return ssHub.state.pageCount;
		},
		set: function(v) {
		    ssHub.state.pageCount = v;
		}
	});
    Object.defineProperty(fujitsu.scansnap, "paperSize", {
		get: function() {
		    return ssHub.state.paperSize;
		},
		set: function(v) {
		    ssHub.state.paperSize = v;
		}
	});
    Object.defineProperty(fujitsu.scansnap, "scanningSide", {
		get: function() {
		    return ssHub.state.scanningSide;
		},
		set: function(v) {
		    ssHub.state.scanningSide = v;
		}
	});
    Object.defineProperty(fujitsu.scansnap, "colorMode", {
		get: function() {
		    return ssHub.state.colorMode;
		},
		set: function(v) {
		    ssHub.state.colorMode = v;
		}
	});
    Object.defineProperty(fujitsu.scansnap, "scanMode", {
		get: function() {
		    return ssHub.state.scanMode;
		},
		set: function(v) {
		    ssHub.state.scanMode = v;
		}
	});
    Object.defineProperty(fujitsu.scansnap, "rotation", {
		get: function() {
		    return ssHub.state.rotation;
		},
		set: function(v) {
		    ssHub.state.rotation = v;
		}
	});
    Object.defineProperty(fujitsu.scansnap, "threshold", {
		get: function() {
		    return ssHub.state.threshold;
		},
		set: function(v) {
		    ssHub.state.threshold = v;
		}
    });
    Object.defineProperty(fujitsu.scansnap, "scanType", {
        get: function () {
            return ssHub.state.scanType;
        },
        set: function (v) {
            ssHub.state.scanType = v;
        }
    });

    Object.defineProperty(fujitsu.scansnap, "serialNo", {
        get: function () {
            return ssHub.state.serialNo;
        },
    });

    Object.defineProperty(fujitsu.scansnap, "cutType", {
        get: function () {
            return ssHub.state.cutType;
        },
        set: function (v) {
            ssHub.state.cutType = v;
        }
    });

    Object.defineProperty(fujitsu.scansnap, "cutLength", {
        get: function () {
            return ssHub.state.cutLength;
        },
        set: function (v) {
            ssHub.state.cutLength = v;
        }
    });
    Object.defineProperty(fujitsu.scansnap, "compression", {
        get: function () {
            return ssHub.state.compression;
        },
        set: function (v) {
            ssHub.state.compression = v;
        }
    });
    Object.defineProperty(fujitsu.scansnap, "cid", {
        get: function () {
            return ssHub.state.cid;
        },
        set: function (v) {
            ssHub.state.cid = v;
        }
    });

    Object.defineProperty(fujitsu.scansnap, "startScanTime", {
        get: function () {
            return ssHub.state.startScanTime;
        },
        set: function (v) {
            ssHub.state.startScanTime = v;
        }
    });

    Object.defineProperty(fujitsu.scansnap, "timerScanTime", {
        get: function () {
            return ssHub.state.timerScanTime;
        },
        set: function (v) {
            ssHub.state.timerScanTime = v;
        }
    });

    Object.defineProperty(fujitsu.scansnap, "fileNameFormat", {
        get: function () {
            return ssHub.state.fileNameFormat;
        },
        set: function (v) {
            ssHub.state.fileNameFormat = v;
        }
    });

    Object.defineProperty(fujitsu.scansnap, "fileCounterDigits", {
        get: function () {
            return ssHub.state.fileCounterDigits;
        },
        set: function (v) {
            ssHub.state.fileCounterDigits = v;
        }
    });

    Object.defineProperty(fujitsu.scansnap, "continueScanMode", {
        get: function () {
            return ssHub.state.continueScanMode;
        },
        set: function (v) {
            ssHub.state.continueScanMode = v;
        }
    });


	var ssHub;
	var convertToBase64Success, convertToBase64Failure;
	var convertToBlobSuccess, convertToBlobFailure;

	function $() {
	    console.log("initiate.js library is called.");
	};

	function Initialize(c) {
		ssHub = c;
		ssHub.client.onConnected = function (id) {
		    console.log("Establish a connection with the server" + " |" + id);


		    if (typeof (eval(ssHub.server.getProperty)) == "function") {
		        console.log("ssHub.server.getProperty defined");
		        ssHub.server.getProperty().done(function () {
		            contextId = fujitsu.scansnap.cid;
		            console.info('getProperty end ' + fujitsu.scansnap.scanMode);
		            
		            if (typeof (eval(ssHub.client.OnScannerReady)) == "function") {
		        		console.log("ssHub.client.OnScannerReady defined");
		        		ssHub.client.OnScannerReady();
		    		}
		    		else {
		        		console.log("ssHub.client.OnScannerReady undefined");
		    		}
		    
		        });
		    }
		    else {
		        console.log("ssHub.server.getProperty undefined");
		    };


		};
		c.client.OnConvertToBase64Success = OnConvertToBase64SuccessAdpter;
		c.client.OnConvertToBase64Failure = OnConvertToBase64FailureAdpter;
		c.client.OnConvertToBlobSuccess = OnConvertToBlobSuccessAdpter;
		c.client.OnConvertToBlobFailure = OnConvertToBlobFailureAdpter;
	}

	function Destroy() {
    ssHub = null;
  }

	function OpenScanner() {

	    console.info("OpenScanner start");

	    return ssHub.server.openScanner();
	};

	function StartScan() {

	    console.info("StartScan start");

	    return ssHub.server.startScan();
	};

	function CloseScanner() {

	    console.info("CloseScanner start");

	    return ssHub.server.closeScanner();
	};

	function GetSourceCount() {

	    console.info("GetSourceCount start");

	    return ssHub.server.getSourceCount();
	};
	
	function GetSourceName(i) {

	    console.info("GetSourceName start" + i);

	    return ssHub.server.getSourceName(i);
	};

	function GetProperty() {

	    console.info("GetProperty start");

	    return ssHub.server.getProperty();
	};

	function GetSourceSelect() {

	    console.info("GetSourceSelect start");

	    return ssHub.server.getSourceSelect();
	};

	function SelectSourceName(n) {

	    console.info("SelectSourceName start");

	    return ssHub.server.selectSourceName(n);
	};

	function SetProperty() {

	    console.info("SetProperty start");

	    return ssHub.server.setProperty();
	};

	function RegisterEvent(n, f) {

	    console.info("RegisterEvent start");
	    if (n == "OnScanToFile") {
	        ssHub.client.OnScanToFile = f;
	    }
	    else if (n == "OnScanToFileEnd") {
	        ssHub.client.OnScanToFileEnd = f;
	    }
	    else if (n == "OnScannerReady") {
	        ssHub.client.OnScannerReady = f;
	    }
	};
	
	function UnRegisterEvent(n) {

	    console.info("UnRegisterEvent start");
	    if (n == "OnScanToFile") {
	        ssHub.client.OnScanToFile = null;
	    }
	    else if (n == "OnScanToFileEnd") {
	        ssHub.client.OnScanToFileEnd = null;
	    }
	    else if (n == "OnScannerReady") {
	        ssHub.client.OnScannerReady = null;
	    }
	};
	
	function ConvertToBase64(d, e) {
	    convertToBase64Success = e;
	    return ssHub.server.convertToBase64(d);
	};
	
	function OnConvertToBase64SuccessAdpter(r, d, f) {
	    console.info("OnConvertToBase64Success" + r);
	    convertToBase64Success(r, d, f);
	};
	
	function OnConvertToBase64FailureAdpter(e) {
	    console.info("OnConvertToBase64Failure" + e);
	};
	
	function ConvertToBlob(d, e) {
	    convertToBlobSuccess = e;
	    return ssHub.server.convertToBlob(d);
	};
	
	function OnConvertToBlobSuccessAdpter(r, d) {
	    console.info("OnConvertToBlobSuccess" + r);
	    convertToBlobSuccess(r, d);
	};
	
	function OnConvertToBlobFailureAdpter(e) {
	    console.info("OnConvertToBlobFailure" + e);
	    fujitsu.scansnap.errorCode = e;
	};

	window['fujitsu']['scansnap']['$'] = $;
	window['fujitsu']['scansnap']['Initialize'] = Initialize;
  window['fujitsu']['scansnap']['Destroy'] = Destroy;
	window['fujitsu']['scansnap']['RegisterEvent'] = RegisterEvent;
	window['fujitsu']['scansnap']['UnRegisterEvent'] = UnRegisterEvent;
	window['fujitsu']['scansnap']['ConvertToBlob'] = ConvertToBlob;
	window['fujitsu']['scansnap']['ConvertToBase64'] = ConvertToBase64;
	window['fujitsu']['scansnap']['OpenScanner'] = OpenScanner;
	window['fujitsu']['scansnap']['StartScan'] = StartScan;
	window['fujitsu']['scansnap']['CloseScanner'] = CloseScanner;
	window['fujitsu']['scansnap']['GetProperty'] = GetProperty;
	window['fujitsu']['scansnap']['SetProperty'] = SetProperty;

})();
